package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import service.UserManager;
import service.impl.UserManagerImpl;
import domain.LUser;
import domain.User;

public class LLoginAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

		LUser userform = (LUser) form;
		UserManager um = new UserManagerImpl();
		System.out.println(userform.getLusername());
		System.out.println(userform.getLpassword());
		User u = um.Llogin(userform);
		if (u != null) {
			request.getSession().setAttribute("LOGIN_USER_ID", u.getId());
			request.getSession().setAttribute("LOGIN_USER_Name", u.getUsername());
			request.getSession().setAttribute("LOGIN_USER_TYPE", ""+u.getType());
			request.getSession().setAttribute("pass", "1");
			request.setAttribute("msg", um.getMsg());

			if (request.getSession().getAttribute("PRE_URL") != null) {

				String pre_url = "" + request.getSession().getAttribute("PRE_URL");
				System.out.println(pre_url);
				// request.getSession().setAttribute("PRE_URL", null);
				return new ActionForward(pre_url, true);
			} else {
				return mapping.findForward("LloginSuccess");
			}
		} else {
			System.out.println("-2-2-2-2-2-2");
			request.setAttribute("msg", um.getMsg());
			return mapping.findForward("LloginFailure");
		}
	}
}
